{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Lesson 5\n",
    "\n",
    "Python Basic, Lesson 5, v1.0.1, 2016.12 by David.Yi    \n",
    "Python Basic, Lesson 5, v1.0.2, 2017.03 modified by Yimeng.Zhang    \n",
    "\n",
    "\n",
    "### 上次内容要点\n",
    "\n",
    "* 日期库 datetime 用法介绍，datetime、time 等库的介绍，获得日期，字符串和日期转换，日期格式介绍，日期加减计算\n",
    "* 函数不同参数形式介绍和举例 \n",
    "* 匿名函数 lambda\n",
    "    \n",
    "### 本次内容要点\n",
    "\n",
    "* 文件和目录操作之一：文件和目录操作\n",
    "* 文件和目录操作之二：读写文本文件\n",
    "* 搜索硬盘上指定路径指定类型的文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter a number: 10\n",
      "The factors of 10 are:\n",
      "1\n",
      "2\n",
      "5\n",
      "10\n",
      "0.0\n"
     ]
    }
   ],
   "source": [
    "# 寻找数字的正因数\n",
    "\n",
    "import time\n",
    "\n",
    "def print_factors(x):\n",
    "    print(\"The factors of\",x,\"are:\")\n",
    "    for i in range(1, x + 1):\n",
    "        if x % i == 0:\n",
    "            print(i)\n",
    "\n",
    "# take input from the user\n",
    "num = int(input(\"Enter a number: \"))\n",
    "\n",
    "t1 = time.time()\n",
    "print_factors(num)\n",
    "t2 = time.time()\n",
    "print(t2-t1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "# 找到最大数 #1\n",
    "\n",
    "def find_max(l):\n",
    "    \n",
    "    # max = float('-inf') 表示负无穷大\n",
    "    max = float('-inf')\n",
    "    for x in l:\n",
    "        if x > max:\n",
    "            max = x\n",
    "    return max\n",
    "\n",
    "print(find_max([-20,1,6,7,20,5]))\n",
    "print(find_max([-20,-3,-6,-7,-5]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20\n"
     ]
    }
   ],
   "source": [
    "# 找到最大数 #2\n",
    "# 使用递归\n",
    "\n",
    "def find_max(l):\n",
    "    if len(l) == 1:\n",
    "        return l[0]\n",
    "    v1 = l[0]\n",
    "    v2 = find_max(l[1:])\n",
    "    if v1 > v2:\n",
    "        return v1\n",
    "    else:\n",
    "        return v2\n",
    "\n",
    "print(find_max([1,6,7,20,5]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "## 文件和目录操作之一\n",
    "\n",
    "Python 的 os 库：有很多和操作系统相关的功能，还有很多和文件、路径和执行系统命令相关的函数。\n",
    "\n",
    "##### os 库常用函数\n",
    "\n",
    "* os.sep 可以取代操作系统特定的路径分割符\n",
    "* os.name 字符串指示你正在使用的平台。比如对于Windows，它是'nt'，而对于Linux/Unix用户，它是'posix' \n",
    "* os.getcwd() 函数得到当前工作目录，即当前Python脚本工作的目录路径\n",
    "* os.chdir(dirname)  改变工作目录到dirname\n",
    "* os.getenv() 用来读取环境变量\n",
    "* os.putenv() 用来设置环境变量 \n",
    "* os.listdir() 返回指定目录下的所有文件和目录名 \n",
    "* os.remove() 删除一个文件\n",
    "* os.system() 运行shell命令\n",
    "* os.linesep 字符串给出当前平台使用的行终止符。例如，Windows使用'/r/n'，Mac使用'\\n'。\n",
    "* os.mkdir() 建立路径\n",
    "* os.rmdir() 删除路径\n",
    "\n",
    ">注意：我们在 /Users/用户名 路径下建立一个用来测试的文件 test.txt\n",
    "\n",
    ">不同操作系统在路径和文件处理上有一定差异，这里的举例是基于 macOS，在 windows 下面会有较大差异，但是用法是一致的\n",
    "\n",
    "##### 关于文件系统的延展阅读\n",
    "* 文件系统介绍 https://zh.wikipedia.org/wiki/%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F\n",
    "* windows 文件系统 FAT、FAT32、NTFS 介绍 https://support.microsoft.com/zh-cn/kb/100108\n",
    "* linux 文件系统介绍 http://cn.linux.vbird.org/linux_basic/0230filesystem.php"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\\\n",
      "nt\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "\n",
    "# 操作系统路径分隔符\n",
    "print(os.sep)\n",
    "\n",
    "# 操作系统平台名称\n",
    "print(os.name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'C:\\\\Users\\\\yimeng.zhang\\\\Desktop\\\\Class\\\\python基础\\\\python_basic'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取当前路径\n",
    "os.getcwd()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'C:\\\\Users\\\\yimeng.zhang\\\\Desktop\\\\Class'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 切换路径\n",
    "\n",
    "# os.chdir('/Users/david.yi')\n",
    "os.chdir('C:\\\\Users\\\\yimeng.zhang\\\\Desktop\\\\Class')\n",
    "os.getcwd()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['.ipynb_checkpoints',\n",
       " 'imgs',\n",
       " 'list_dump.txt',\n",
       " 'python_basic_lesson_01.ipynb',\n",
       " 'python_basic_lesson_02.ipynb',\n",
       " 'python_basic_lesson_03.ipynb',\n",
       " 'python_basic_lesson_04.ipynb',\n",
       " 'python_basic_lesson_05.ipynb',\n",
       " 'python_basic_lesson_06.ipynb',\n",
       " 'python_basic_outline.md',\n",
       " 'python_basic_outline.pdf',\n",
       " 'test.txt',\n",
       " 'test2.txt',\n",
       " 'tmp.ipynb',\n",
       " 'Untitled.ipynb',\n",
       " '~$讲义.docx',\n",
       " '讲义.docx']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 返回指定的文件夹包含的文件或文件夹的名字的列表。这个列表以字母顺序。 \n",
    "# 不包括 '.' 和'..' 即使它在文件夹中。\n",
    "\n",
    "import os\n",
    "\n",
    "# windows\n",
    "os.chdir('C:\\\\Users\\\\yimeng.zhang\\\\Desktop\\\\Class\\\\python基础\\\\python_basic')\n",
    "os.listdir()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 注意返回的数据类型\n",
    "print(type(os.listdir()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "22\n"
     ]
    }
   ],
   "source": [
    "# 计算目录下有多少文件\n",
    "a = os.listdir()\n",
    "print(len(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "python_basic_lesson_01.ipynb\n",
      "python_basic_lesson_02.ipynb\n",
      "python_basic_lesson_03.ipynb\n",
      "python_basic_lesson_04.ipynb\n",
      "python_basic_lesson_05.ipynb\n",
      "python_basic_lesson_06.ipynb\n",
      "python_basic_outline.md\n",
      "python_basic_outline.pdf\n"
     ]
    }
   ],
   "source": [
    "# 可以指定路径参数，来列出该目录下所有文件\n",
    "\n",
    "# list_a = os.listdir('/Users/david.yi')\n",
    "list_a = os.listdir('C:\\\\Users\\\\yimeng.zhang\\\\Desktop\\\\Class\\\\python基础\\\\python_basic')\n",
    "\n",
    "# 可以判断各类情况，比如第一个是 p 字母\n",
    "for i in list_a:\n",
    "    if i[0:1] == 'p':\n",
    "        print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\r\\n'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 操作系统换行符\n",
    "# 在一些文本文件处理中有用\n",
    "\n",
    "os.linesep"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ok\n"
     ]
    }
   ],
   "source": [
    "# 建立路径\n",
    "\n",
    "# os.chdir('/Users/david.yi')\n",
    "os.chdir('C:\\\\Users\\\\yimeng.zhang\\\\Desktop\\\\Class\\\\python基础\\\\python_basic')\n",
    "os.mkdir('test')\n",
    "print('ok')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "##### os.path 常用函数之一\n",
    "\n",
    "* os.path.isdir() 检查给出的路径是否是一个目录\n",
    "* os.path.isfile() 检查给出的路径是否一个文件\n",
    "* os.path.exists() 检查给出的路径或者文件是否存在\n",
    "* os.path.getsize() 获得路径或者文件的大小\n",
    "* os.path.getatime() 返回所指向的文件或者目录的最后存取时间\n",
    "* os.path.getmtime() 返回所指向的文件或者目录的最后修改时间 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\n",
      "True\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# 检查给出的路径是否是一个存在的目录，存在\n",
    "\n",
    "# os.chdir('/Users/david.yi/Documents/dev/python_study/python_basic')\n",
    "os.chdir('C:\\\\Users\\\\yimeng.zhang\\\\Desktop\\\\Class\\\\python基础\\\\python_basic')\n",
    "s_dir = os.getcwd()\n",
    "print(s_dir)\n",
    "print(os.path.isdir(s_dir))\n",
    "print(os.path.isdir('C:\\\\Users'))\n",
    "print(os.path.isdir('C:\\\\Users222'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 检查给出的路径是否是一个存在的目录，不存在\n",
    "os.path.isdir(s_dir + 's')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 文件不是路径，所以返回 False\n",
    "os.path.isdir(s_dir + 'test.txt')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\test.txt\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 检查给出的路径是否一个文件，存在\n",
    "\n",
    "s_dir = os.getcwd()\n",
    "s_file = os.path.join(s_dir, 'test.txt')\n",
    "print(s_file)\n",
    "os.path.isfile(s_file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 检查给出的路径是否一个文件，不存在\n",
    "\n",
    "s_dir = os.getcwd()\n",
    "s_file = os.path.join(s_dir, 'test222.txt')\n",
    "os.path.isfile(s_file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 路径不是文件，所以返回 False\n",
    "\n",
    "s_dir = os.getcwd()\n",
    "os.path.isfile(s_dir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "# 对路径和文件都通用的检查方式\n",
    "\n",
    "s_dir = os.getcwd()\n",
    "s_file = os.path.join(s_dir, 'test.txt')\n",
    "\n",
    "print(os.path.exists(s_dir))\n",
    "print(os.path.exists(s_file))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "17"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获得路径或者文件的大小\n",
    "\n",
    "s_dir = os.getcwd()\n",
    "s_file = os.path.join(s_dir, 'test.txt')\n",
    "\n",
    "os.path.getsize(s_file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4096"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获得路径或者文件的大小\n",
    "\n",
    "os.path.getsize(s_dir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1490680273.8670404"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 返回所指向的文件或者目录的最后存取时间\n",
    "\n",
    "s_dir = os.getcwd()\n",
    "s_file = os.path.join(s_dir, 'test.txt')\n",
    "\n",
    "os.path.getatime(s_file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2017-03-28 15:01:40\n"
     ]
    }
   ],
   "source": [
    "# 返回所指向的文件或者目录的最后存取时间\n",
    "import os\n",
    "import time\n",
    "\n",
    "# 将日期格式化\n",
    "dt = time.localtime(os.path.getatime(s_dir))\n",
    "# print(dt)\n",
    "print(time.strftime('%Y-%m-%d %H:%M:%S', dt))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1490681200.9609938"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 返回所指向的文件或者目录的最后修改时间\n",
    "\n",
    "s_file = os.path.join(s_dir, 'test.txt')\n",
    "\n",
    "os.path.getmtime(s_file) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Tue Mar 28 13:51:13 2017'"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 返回所指向的文件或者目录的最后修改时间\n",
    "# 使用 time.ctime() 方法来格式化日期\n",
    "\n",
    "import time, os\n",
    "\n",
    "s_file = os.path.join(s_dir, 'test.txt')\n",
    "\n",
    "time.ctime(os.path.getmtime(s_file))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "##### os.path 常用函数之二\n",
    "\n",
    "* os.path.split() 返回一个路径的目录名和文件名\n",
    "* os.path.abspath() 返回规范化的绝对路径\n",
    "* os.path.isabs() 如果输入是绝对路径，返回True\n",
    "* os.path.split() 将路径分割成目录和文件名的二元素元组\n",
    "* os.path.splitdrive() 返回（drivername，fpath）元组 \n",
    "* os.path.dirname() 返回路径的目录，其实就是 os.path.split(path)的第一个元素\n",
    "* os.path.basename() 返回路径最后的文件名，其实就是 os.path.split(path)的第二个元素\n",
    "* os.path.splitext() 分离文件名与扩展名，返回(fname,fextension)元组 \n",
    "* os.path.join() 将多个路径组合后返回，第一个绝对路径之前的参数将被忽略\n",
    "* os.path.commonprefix(list) 返回list中，所有路径共有的最长的路径"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\test.txt\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "('C:\\\\Users\\\\yimeng.zhang\\\\Desktop\\\\Class\\\\python基础\\\\python_basic', 'test.txt')"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 返回一个路径的目录名和文件名\n",
    "\n",
    "# os.chdir('/Users/david.yi/Documents/dev/python_study/python_basic')\n",
    "os.chdir('C:\\\\Users\\\\yimeng.zhang\\\\Desktop\\\\Class\\\\python基础\\\\python_basic')\n",
    "s_dir = os.getcwd()\n",
    "s_file = os.path.join(s_dir, 'test.txt')\n",
    "print(s_file)\n",
    "os.path.split(s_file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'C:\\\\Users\\\\yimeng.zhang\\\\Desktop\\\\Class\\\\python基础\\\\python_basic\\\\tt1211.txt'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 返回规范化的绝对路径\n",
    "# 会自动补齐完整路径\n",
    "\n",
    "os.path.abspath('tt1211.txt') "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "# 如果输入是绝对路径，返回True\n",
    "\n",
    "print(os.path.isabs('test.txt'))\n",
    "print(os.path.isabs('/Users/yijun/test.txt'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('C:\\\\Users\\\\yimeng.zhang\\\\Desktop\\\\Class\\\\python基础\\\\python_basic', 'test.txt')\n",
      "<class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "# 将路径分割成目录和文件名的二元素元组\n",
    "\n",
    "s = os.path.split(s_file)\n",
    "print(s)\n",
    "print(type(s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/Users/yijun'"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 返回路径的目录，其实就是 os.path.split(path)的第一个元素\n",
    "\n",
    "os.path.dirname('/Users/yijun/test.txt')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'test.txt'"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 返回路径最后的文件名，其实就是 os.path.split(path)的第二个元素\n",
    "\n",
    "os.path.basename(s_file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('C:\\\\Users\\\\yimeng.zhang\\\\Desktop\\\\Class\\\\python基础\\\\python_basic\\\\test',\n",
       " '.txt')"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 分离文件名与扩展名，返回(fname,fextension)元组 \n",
    "\n",
    "os.path.splitext(s_file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'C:\\\\Users\\\\yimeng.zhang\\\\Desktop\\\\Class\\\\python基础\\\\python_basic\\\\test.txt'"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将多个路径组合后返回，第一个绝对路径之前的参数将被忽略\n",
    "\n",
    "# os.path.join('/Users/yijun', 'test.txt')\n",
    "os.path.join('C:\\\\Users\\\\yimeng.zhang\\\\Desktop\\\\Class\\\\python基础\\\\python_basic', 'test.txt')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/Users/yijun/'"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 返回list中，所有路径共有的最长的路径\n",
    "\n",
    "l = ['/Users/yijun/test.txt', '/Users/yijun/test/aaa.txt', '/Users/yijun/bbb.txt']\n",
    "os.path.commonprefix(l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\.ipynb_checkpoints\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\.ipynb_checkpoints\\python_basic_lesson_01-checkpoint.ipynb\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\.ipynb_checkpoints\\python_basic_lesson_02-checkpoint.ipynb\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\.ipynb_checkpoints\\python_basic_lesson_03-checkpoint.ipynb\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\.ipynb_checkpoints\\python_basic_lesson_04-checkpoint.ipynb\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\.ipynb_checkpoints\\python_basic_lesson_05-checkpoint.ipynb\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\.ipynb_checkpoints\\python_basic_lesson_06-checkpoint.ipynb\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\.ipynb_checkpoints\\tmp-checkpoint.ipynb\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\.ipynb_checkpoints\\Untitled-checkpoint.ipynb\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\.ipynb_checkpoints\\Untitled1-checkpoint.ipynb\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\1.png\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\imgs\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\imgs\\anaconda.png\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\imgs\\jupyter.png\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\imgs\\pycharm.png\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\imgs\\python_install.jpeg\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\imgs\\python_install_1.jpeg\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\imgs\\python_install_2.jpeg\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\imgs\\python_install_3.jpeg\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\imgs\\python_install_4.jpeg\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\imgs\\python_install_5.png\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\imgs\\python_use.jpg\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\lesson1.txt\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\list_dump.txt\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\python6.txt\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\python_basic_lesson_01.ipynb\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\python_basic_lesson_02.ipynb\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\python_basic_lesson_03.ipynb\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\python_basic_lesson_04.ipynb\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\python_basic_lesson_05.ipynb\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\python_basic_lesson_06.ipynb\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\python_basic_outline.md\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\python_basic_outline.pdf\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\test\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\test.txt\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\test2.txt\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\tmp.ipynb\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\Untitled.ipynb\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\Untitled1.ipynb\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\~$讲义.docx\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\讲义.docx\n"
     ]
    }
   ],
   "source": [
    "# 遍历一个目录下的所有文件\n",
    "\n",
    "import os \n",
    "\n",
    "def list_dir(root_dir): \n",
    "    for lists in os.listdir(root_dir): \n",
    "        path = os.path.join(root_dir, lists) \n",
    "        print(path)\n",
    "        if os.path.isdir(path): \n",
    "            list_dir(path) \n",
    "\n",
    "# 注意不要挑选目录下过多文件的，否则会耗费电脑资源\n",
    "# list_dir('/Users/david.yi/Documents/dev/dig/doc')\n",
    "list_dir('C:\\\\Users\\\\yimeng.zhang\\\\Desktop\\\\Class\\\\python基础\\\\python_basic')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\imgs   4096\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\imgs\\anaconda.png   834508\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\imgs\\jupyter.png   105782\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\imgs\\pycharm.png   192052\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\imgs\\python_install.jpeg   21799\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\imgs\\python_install_1.jpeg   28779\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\imgs\\python_install_2.jpeg   22304\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\imgs\\python_install_3.jpeg   31846\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\imgs\\python_install_4.jpeg   15478\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\imgs\\python_install_5.png   33686\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\imgs\\python_use.jpg   102903\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\list_dump.txt   52\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\python_basic_lesson_01.ipynb   17697\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\python_basic_lesson_02.ipynb   30474\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\python_basic_lesson_03.ipynb   21840\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\python_basic_lesson_04.ipynb   25646\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\python_basic_lesson_05.ipynb   41047\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\python_basic_lesson_06.ipynb   24430\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\python_basic_outline.md   1271\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\python_basic_outline.pdf   63096\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\test.txt   0\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\tmp.ipynb   1275\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\Untitled.ipynb   2468\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\~$讲义.docx   162\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\讲义.docx   47848\n"
     ]
    }
   ],
   "source": [
    "# 遍历一个目录下的所有文件\n",
    "# 显示文件的字节数，用 getsize() \n",
    "\n",
    "import os \n",
    "\n",
    "def list_dir(root_dir): \n",
    "    for lists in os.listdir(root_dir): \n",
    "        path = os.path.join(root_dir, lists) \n",
    "        if lists[0:1] != '.': \n",
    "            filesize = os.path.getsize(path)\n",
    "            print(path, ' ', filesize)\n",
    "            if os.path.isdir(path): \n",
    "                list_dir(path) \n",
    "\n",
    "# 注意不要挑选目录下过多文件的，否则会耗费电脑资源\n",
    "#list_dir('/Users/david.yi/Documents/dev/dig/doc')\n",
    "list_dir('C:\\\\Users\\\\yimeng.zhang\\\\Desktop\\\\Class\\\\python基础\\\\python_basic')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# 遍历一个目录下的所有文件\n",
    "# 过滤 . 开头的文件，一般是系统文件\n",
    "# 显示文件的字节数\n",
    "# 显示指定后缀的文件，引入 endswith 用法\n",
    "\n",
    "import os \n",
    "\n",
    "def list_dir(root_dir): \n",
    "    for lists in os.listdir(root_dir): \n",
    "        path = os.path.join(root_dir, lists) \n",
    "        if lists[0:1] != '.' and lists.endswith('.txt'):\n",
    "            filesize = os.path.getsize(path)\n",
    "            print(path, ' ', filesize)\n",
    "            if os.path.isdir(path): \n",
    "                list_dir(path) \n",
    "\n",
    "# 注意不要挑选目录下过多文件的，否则会耗费电脑资源\n",
    "\n",
    "# list_dir('/Users/david.yi/Documents/dev/dig/n_query')\n",
    "list_dir('C:\\\\Users\\\\yimeng.zhang\\\\Desktop\\\\Class\\\\python基础\\\\python_basic')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['C:\\\\Users\\\\yimeng.zhang\\\\Desktop\\\\Class\\\\python基础\\\\python_basic\\\\imgs\\\\python_use.jpg', 'C:\\\\Users\\\\yimeng.zhang\\\\Desktop\\\\Class\\\\python基础\\\\python_basic\\\\imgs\\\\python_install.jpeg', 'C:\\\\Users\\\\yimeng.zhang\\\\Desktop\\\\Class\\\\python基础\\\\python_basic\\\\imgs\\\\python_install_1.jpeg', 'C:\\\\Users\\\\yimeng.zhang\\\\Desktop\\\\Class\\\\python基础\\\\python_basic\\\\imgs\\\\python_install_2.jpeg', 'C:\\\\Users\\\\yimeng.zhang\\\\Desktop\\\\Class\\\\python基础\\\\python_basic\\\\imgs\\\\python_install_3.jpeg', 'C:\\\\Users\\\\yimeng.zhang\\\\Desktop\\\\Class\\\\python基础\\\\python_basic\\\\imgs\\\\python_install_4.jpeg', 'C:\\\\Users\\\\yimeng.zhang\\\\Desktop\\\\Class\\\\python基础\\\\python_basic\\\\imgs\\\\anaconda.png', 'C:\\\\Users\\\\yimeng.zhang\\\\Desktop\\\\Class\\\\python基础\\\\python_basic\\\\imgs\\\\jupyter.png', 'C:\\\\Users\\\\yimeng.zhang\\\\Desktop\\\\Class\\\\python基础\\\\python_basic\\\\imgs\\\\pycharm.png', 'C:\\\\Users\\\\yimeng.zhang\\\\Desktop\\\\Class\\\\python基础\\\\python_basic\\\\imgs\\\\python_install_5.png']\n"
     ]
    }
   ],
   "source": [
    "# 写一个可以搜索硬盘上指定路径指定类型的文件\n",
    "# os.walk() 返回一个三元tupple(root, dirnames, filenames)\n",
    "# 第一个为起始路径，String\n",
    "# 第二个为起始路径下的文件夹, List\n",
    "# 第三个是起始路径下的文件. List\n",
    "\n",
    "import fnmatch\n",
    "import os\n",
    "\n",
    "images = ['*.jpg', '*.jpeg', '*.png', '*.tif', '*.tiff']\n",
    "matches = []\n",
    "\n",
    "# for root, dirnames, filenames in os.walk('/Users/david.yi/Documents/dev/'):\n",
    "for root, dirnames, filenames in os.walk('C:\\\\Users\\\\yimeng.zhang\\\\Desktop\\\\Class\\\\python基础\\\\python_basic'):\n",
    "    for extensions in images:\n",
    "        for filename in fnmatch.filter(filenames, extensions):\n",
    "            matches.append(os.path.join(root, filename))\n",
    "\n",
    "print(matches)\n",
    "# import os\n",
    "# for root, dirnames, filenames in os.walk('C:\\\\Users\\\\yimeng.zhang\\\\Desktop\\\\Class\\\\python基础\\\\python_basic'):\n",
    "#     print(filenames)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "## 文件和目录操作之二\n",
    "\n",
    "读写文件是最常见的IO操作。Python内置了读写文件的函数，用法和C是兼容的。\n",
    "\n",
    "读写文件前，我们先必须了解一下，在磁盘上读写文件的功能都是由操作系统提供的，现代操作系统不允许普通的程序直接操作磁盘，所以，读写文件就是请求操作系统打开一个文件对象，然后，通过操作系统提供的接口从这个文件对象中读取数据，或者把数据写入这个文件对象。\n",
    "\n",
    "##### 读文件\n",
    "\n",
    "函数 `open()` 返回 文件对象，通常的用法需要两个参数：`open(filename, mode)`。分别是文件名和打开模式\n",
    "\n",
    "在做下面的例子前，我们要创建一个 `test.txt` 文件，并且保证其中的内容是如下样式，包含三行内容：\n",
    "\n",
    "> hello\n",
    "\n",
    "> hi\n",
    "\n",
    "> byebye\n",
    "\n",
    "文件保存在可以访问的目录，我这里就保存在和 notebook 同样的目录\n",
    "\n",
    "> 使用 jupyter 可以直接新建 Text File，来完成建立和编辑文本文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\test.txt\n",
      "hello\n",
      "hi\n",
      "byebye\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "\n",
    "# 获得当前路径\n",
    "# os.chdir('/Users/david.yi/Documents/dev/python_study/python_basic')\n",
    "s_dir = os.getcwd()\n",
    "\n",
    "print(s_dir)\n",
    "\n",
    "# 拼接完整文件名\n",
    "filename = os.path.join(s_dir, 'test.txt')\n",
    "\n",
    "print(filename)\n",
    "\n",
    "try:\n",
    "    # 打开文件\n",
    "    f = open(filename, 'r')\n",
    "    print(f.read())\n",
    "finally:\n",
    "    if f:\n",
    "        f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n",
      "hi\n",
      "byebye\n"
     ]
    }
   ],
   "source": [
    "# 简化调用方式\n",
    "# 省却了 try...finally，会有 with 来自动控制\n",
    "\n",
    "with open(filename, 'r') as f:\n",
    "    print(f.read())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'list'>\n",
      "['hello\\n', 'hi\\n', 'byebye']\n"
     ]
    }
   ],
   "source": [
    "with open(filename, 'r') as f:\n",
    "    lines = f.readlines()\n",
    "\n",
    "print(type(lines))\n",
    "print(lines)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n",
      "\n",
      "hi\n",
      "\n",
      "byebye\n"
     ]
    }
   ],
   "source": [
    "for i in lines:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n",
      "\n",
      "hi\n",
      "\n",
      "byebye\n"
     ]
    }
   ],
   "source": [
    "# 更简单的按行读取文件内容方法\n",
    "with open(filename, 'r') as f:\n",
    "    for eachline in f:\n",
    "        print(eachline)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "##### 写文件\n",
    "\n",
    "写文件和读文件是一样的，唯一区别是调用 `open()` 函数时，传入标识符 `'w'` 或者 `'wb'` 表示写文本文件或写二进制文件。\n",
    "\n",
    "r 以读方式打开\n",
    "w 以写方式打开\n",
    "a 以追加模式打开（必要时候创建新文件）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\test2.txt\n",
      "Hello, World!\n",
      "\n",
      "Hello, Shanghai!\n",
      "\n",
      "Hello, CHINA!\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 写文件\n",
    "import os\n",
    "\n",
    "# 获得当前路径\n",
    "# os.chdir('/Users/david.yi/Documents/dev/python_study/python_basic')\n",
    "s_dir = os.getcwd()\n",
    "\n",
    "# 拼接完整文件名\n",
    "filename= os.path.join(s_dir, 'test2.txt')\n",
    "print(filename)\n",
    "\n",
    "# 换行符\n",
    "br = os.linesep\n",
    "\n",
    "# 写文件\n",
    "with open(filename, 'w') as f:\n",
    "#     f.write('Hello, World!' + br)\n",
    "#     f.write('Hello, Shanghai!' + br)\n",
    "#     f.write('Hello, CHINA!' + br)\n",
    "    f.close()\n",
    "    \n",
    "with open(filename, 'r') as f:\n",
    "    print(f.read())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "##### 操作系统和文件系统差异处理\n",
    "\n",
    "`linesep` 文件中分隔行的字符串\n",
    "`path.sep` 分割文件路径名的字符串\n",
    "`curdir` 当前工作目录的字符串\n",
    "`pardir` 当前工作目录的父目录字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "##### 使用 glob 包查找文件\n",
    "\n",
    "glob 是 python 自己带的一个文件操作相关模块，很简洁，用它可以查找符合自己目的的文件，就类似于Windows下的文件搜索，而且也支持通配符: `*,?,[]` 这三个通配符，\\* 代表0个或多个字符，? 代表一个字符，[] 匹配指定范围内的字符，如[0-9]匹配数字。\n",
    "\n",
    "glob 的主要方法也叫 glob，该方法返回所有匹配的文件路径列表，该方法需要一个参数用来指定匹配的路径字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\*\\*.png\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\imgs\\anaconda.png\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\imgs\\jupyter.png\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\imgs\\pycharm.png\n",
      "C:\\Users\\yimeng.zhang\\Desktop\\Class\\python基础\\python_basic\\imgs\\python_install_5.png\n"
     ]
    }
   ],
   "source": [
    "# 使用 glob 来遍历指定路径下的指定类型文件\n",
    "import os, glob\n",
    "\n",
    "# 获得当前路径\n",
    "# os.chdir('/Users/david.yi/Documents/dev/')\n",
    "s_dir = os.getcwd()\n",
    "\n",
    "s_find = os.path.join(s_dir, '*', '*.png' )\n",
    "print(s_find)\n",
    "\n",
    "# list_a = glob.glob('/Users/david.yi/Documents/dev/*/*.py')\n",
    "list_a = glob.glob('C:\\\\Users\\\\yimeng.zhang\\\\Desktop\\\\Class\\\\python基础\\\\python_basic\\\\*\\\\*.png')\n",
    "for i in list_a:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "---\n",
    "\n",
    "思考一下：\n",
    "\n",
    "* 修改上面搜索制定类型文件的程序，当搜索到10个文件的时候即停止，并显示\n",
    "* 想一下，如果要搜索指定目录下的 .py 文件，如果py文件中有指定的单词，则显示，并继续搜索"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
